package com.example.drive.car.controller;

import com.example.drive.car.pojo.Car;
import com.example.drive.car.pojo.CarDTO;
import com.example.drive.car.service.CarService;
import com.example.drive.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 车辆控制层
 */
@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * 获取车辆列表
     * @return
     */
    @GetMapping("list")
    public R<List<Car>> getCarList(){
        List<Car> carList = carService.getList();
        return R.ok(carList);
    }

    /**
     * 添加车辆
     * @param carDTO
     * @return
     */
    @PostMapping("add")
    public R<Void> addCar(@RequestBody @Validated CarDTO carDTO){
        carService.add(carDTO);
        return R.ok();
    }

    /**
     * 删除车辆
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> deleteCar(@PathVariable Integer id){
        carService.delete(id);
        return R.ok();
    }

    /**
     * 更新车辆信息
     */
    @PutMapping("update/{id}")
    public R<Void> updateCar(@RequestBody @Validated CarDTO carDTO, @PathVariable Integer id){
        carService.update(carDTO, id);
        return R.ok();
    }

    /**
     * 上传图片
     * @param image
     * @return
     */
    @PostMapping("uploadImage/{id}")
    public R<String> uploadImage(@PathVariable("id") Integer id, @RequestParam("image") MultipartFile image) {
        carService.uploadImage(id, image);
        return R.ok("图片上传成功");
    }

    /**
     * 查询车辆
     * @param id
     * @return
     */
    @GetMapping("search/{id}")
    public R<Car> searchCar(@PathVariable Integer id){
        Car car = carService.search(id);
        return R.ok(car);
    }

    /**
     * 安排教练
     * @param id
     * @param coach_id
     * @return
     */
    @PostMapping("arrange/{id}")
    public R<Void> arrangeCoach(@PathVariable("id") Integer id, @RequestParam("coach")  Integer coach_id){
        carService.arrange(id,coach_id);
        return R.ok();
    }
}